package com.fastjrun.bms.service.impl;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.fastjrun.bms.dao.BookDao;
import com.fastjrun.bms.dao.PressDao;
import com.fastjrun.bms.dto.BookDTO;
import com.fastjrun.bms.dto.BookQueryDTO;
import com.fastjrun.bms.entity.Book;
import com.fastjrun.bms.entity.BookExample;
import com.fastjrun.bms.entity.Press;
import com.fastjrun.bms.entity.PressExample;
import com.fastjrun.bms.helper.DTOToEntifyConverter;
import com.fastjrun.bms.helper.EntityToVOConverter;
import com.fastjrun.bms.service.BaseDemoService;
import com.fastjrun.bms.service.BookManagerService;
import com.fastjrun.bms.vo.BookVO;
import com.fastjrun.bms.vo.PressVO;
import com.fastjrun.bms.vo.TotalVO;
import com.fastjrun.dto.PageResult;
import com.fastjrun.helper.MockHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 注意：本内容仅限于公司内部传阅，禁止外泄以及用于其他的商业目的 @Copyright 2021 快嘉. All rights reserved.
 *
 * @author cuiyingfeng
 */
@Service("bookManagerService")
public class BookManagerServiceImpl extends BaseDemoService implements BookManagerService {
  @Autowired BookDao bookDao;
  /** 条件分页查询图书列表 */
  @Override
  public PageResult<BookVO> search(BookQueryDTO requestBody) {
    PageResult<BookVO> response = new PageResult();

    String name = requestBody.getName();
    String author = requestBody.getAuthor();
    String isbn = requestBody.getIsbn();
    Long pressId = requestBody.getPressId();
    Integer current = requestBody.getCurrent();
    Integer size = requestBody.getSize();
    if (current == null) {
      current = PAGE_CURRENT;
    }
    if (size == null) {
      size = PAGE_SIZE;
    }
    if (StringUtils.hasText(name)) {
      name = "%" + name + "%";
    }
    if (StringUtils.hasText(author)) {
      author = "%" + author + "%";
    }
    PageHelper.startPage(current, size, true);

    PageInfo<BookVO> pageInfo = new PageInfo<>(bookDao.search(name, author, isbn, pressId));

    response.setRows(pageInfo.getList());
    response.setCurrPage(current);
    response.setPageSize(size);
    response.setTotalPage(pageInfo.getPages());
    response.setTotal((int) pageInfo.getTotal());
    return response;
  }

  /** 查询图书信息 */
  @Override
  public BookVO getById(Long id) {
    Book book = bookDao.selectByPrimaryKey(id);
    return EntityToVOConverter.convert(book);
  }

  /** 新增图书 */
  @Override
  public void add(BookDTO requestBody) {
    Book book = DTOToEntifyConverter.convert(requestBody);
    Date currentDate = new Date(System.currentTimeMillis());
    book.setCreateDate(currentDate);
    book.setUpdateDate(currentDate);
    bookDao.insert(book);
  }

  /** 修改图书 */
  @Override
  public void save(BookDTO requestBody) {
    Book book = DTOToEntifyConverter.convert(requestBody);
    Date currentDate = new Date(System.currentTimeMillis());
    book.setUpdateDate(currentDate);
    bookDao.updateByPrimaryKeySelective(book);
  }

  /** 删除图书信息 */
  @Override
  public void deleteById(Long id) {
    bookDao.deleteByPrimaryKey(id);
  }
}
